﻿
using MediatR;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicetable.Queries
{
    public class GetDevicetableNameQueries : IRequest<Json<List<GetDevicetableNameDTO>>>
    {

    }

    public class GetDevicetableNameQueriesHandler : IRequestHandler<GetDevicetableNameQueries, Json<List<GetDevicetableNameDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServers;
        private readonly IDevicetableRepository _ideviceTableRepository;
        private readonly IUsertableRepository _iusertableRepository;
        public GetDevicetableNameQueriesHandler(ICurrentUserService icurrentUserServers, IDevicetableRepository ideviceTableRepository, IUsertableRepository iusertableRepository)
        {
            _icurrentUserServers = icurrentUserServers;
            _ideviceTableRepository = ideviceTableRepository;
            _iusertableRepository = iusertableRepository;
        }
        public async Task<Json<List<GetDevicetableNameDTO>>> Handle(GetDevicetableNameQueries request, CancellationToken cancellationToken)
        {
            int userid = _icurrentUserServers.GetUserId();
            if (userid <= 0)
            {
                return new Json<List<GetDevicetableNameDTO>>() { Status = 1003, Detail = "请先登录", Data = new List<GetDevicetableNameDTO>() { } };
            }
            else {
                int level = _icurrentUserServers.GetRoletype();
                List<GetDevicetableNameDTO> list = new List<GetDevicetableNameDTO>() { };
                if (level == 2)
                {
                    list = await _ideviceTableRepository.GetDevicetableNameByAdmin(userid, cancellationToken);
                }
                else {
                    list = await _ideviceTableRepository.GetDevicetableNameByUserId(userid, cancellationToken);
                }
                if (list != null && list.Any())
                {
                    return new Json<List<GetDevicetableNameDTO>>() { Status = 1000, Detail = "查询成功", Data = list };
                }
                else {
                    return new Json<List<GetDevicetableNameDTO>>() { Status = 1000, Detail = "暂无数据", Data = new List<GetDevicetableNameDTO>() { } };
                }


            }
        }
    }
}
